package com.ellwood.IPGuard.common.core.utils;

import cn.hutool.core.util.StrUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: IPGuard-boot
 * @PackageName: com.ellwood.IPGuard.common.core.utils
 * @ClassName: HttpContextUtils
 * @Description: // TODO
 * @Author: Ellwood
 * @CreateDate: 2024/10/11 - 上午11:23
 * @Version: V1.0.0
 */
public class HttpContextUtils {
        
        public static HttpServletRequest getHttpServletRequest() {
                RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
                if (requestAttributes == null) {
                        return null;
                }
                
                return ((ServletRequestAttributes) requestAttributes).getRequest();
        }
        
        
        public static HttpServletResponse getHttpServletResponse() {
                RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
                if (requestAttributes == null) {
                        return null;
                }
                
                return ((ServletRequestAttributes) requestAttributes).getResponse();
        }
        
        
        public static Map<String, String> getParameterMap(HttpServletRequest request) {
                Enumeration<String> parameters = request.getParameterNames();
                
                Map<String, String> params = new HashMap<>();
                while (parameters.hasMoreElements()) {
                        String parameter = parameters.nextElement();
                        String value = request.getParameter(parameter);
                        if (StrUtil.isNotBlank(value)) {
                                params.put(parameter, value);
                        }
                }
                
                return params;
        }
        
        public static String getDomain() {
                HttpServletRequest request = getHttpServletRequest();
                
                return getDomain(request);
        }
        
        public static String getDomain(HttpServletRequest request) {
                String domain = request.getHeader(HttpHeaders.ORIGIN);
                if (StrUtil.isBlank(domain)) {
                        domain = request.getHeader(HttpHeaders.REFERER);
                }
                return StringUtils.removeEnd(domain, "/");
        }
        
        public static String getOrigin() {
                HttpServletRequest request = getHttpServletRequest();
                return request.getHeader(HttpHeaders.ORIGIN);
        }
        
}
